package com.quasar.hpatient.dialog;

import android.app.Activity;
import android.text.TextUtils;
import android.widget.EditText;
import android.widget.TextView;

import com.quasar.hpatient.R;

import lib.quasar.base.dialog.BaseDialog;
import lib.quasar.db.manager.DBManager;
import lib.quasar.db.table.User;
import lib.quasar.util.InputUtil;
import lib.quasar.widget.edit.CheckEditText;

public class ChatDialog extends BaseDialog {

    /******************************************************/

    private OnDialogChangeListener listener;

    public ChatDialog(Activity activity) {
        super(activity);
    }

    @Override
    protected int initView() {
        return R.layout.layout_dialog_chat;
    }

    @Override
    protected void initData() {
        findViewById(R.id.dialog_chat_ok).setOnClickListener(v -> cancel());
        findViewById(R.id.dialog_chat_back).setOnClickListener(v -> dismiss());
    }

    @Override
    public void show() {
        super.show();

        final EditText input = findViewById(R.id.dialog_chat_input);
        if (null != input) {
            final User patientModel = DBManager.getInstance().syncGetUserModel();
            final String name = patientModel.getReal_name();
            final String message = getContext().getResources().getString(R.string.dialog_chat_apply, name);
            input.setText(message);
        }

        final CheckEditText text = findViewById(R.id.dialog_chat_input);
        if (null == text) return;
        text.requestFocus();
        InputUtil.openKeybord(text);
    }

    @Override
    public void dismiss() {

        final CheckEditText text = findViewById(R.id.dialog_chat_input);
        if (null != text) {
            InputUtil.closeKeybord(text);
        }

        super.dismiss();
    }

    @Override
    public void cancel() {

        if (null != listener) {

            final CheckEditText text = findViewById(R.id.dialog_chat_input);
            if (null != text) {
                final String trim = text.getText().toString().trim();
                if (!TextUtils.isEmpty(trim)) {
                    listener.onDialogChange(trim);
                }
            }
        }

        super.cancel();
    }

    public final void setTitie(String str) {
        final TextView text = findViewById(R.id.dialog_input_single_title);
        if (null == text) return;
        text.setText(str);
    }

    public void setOnDialogChangeListener(OnDialogChangeListener mListener) {
        this.listener = mListener;
    }

    public interface OnDialogChangeListener {

        void onDialogChange(String input);
    }
}
